﻿using _2._13._2015_VehiclesInClass.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _2._13._2015_VehiclesInClass.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<Vehicle> vehicles = VehicleService.Instance.GetVehicles();

            //List<Vehicle> cars = new List<Vehicle>();

            //Car car = new Car();

            //car.Make = "Honda";
            //car.Model1 = "Accord";
            //car.Year = 2015;

            //cars.Add(car);

            //Motorcycle mot = new Motorcycle();

            //mot.Make = "Harley";
            //mot.Model1 = "Davidson";
            //mot.Year = 2015;

            //cars.Add(mot);

            return View(vehicles);

        }

        public ActionResult Motorcycle()
        {
            List<Vehicle> vehicles = new List<Vehicle>();

            Car car = new Car();

            car.Make = "Honda";
            car.Model1 = "Accord";
            car.Year = 2015;

            vehicles.Add(car);

            Motorcycle mot = new Motorcycle();

            mot.Make = "Harley";
            mot.Model1 = "Davidson";
            mot.Year = 2015;

            vehicles.Add(mot);

            List<Motorcycle> motos = vehicles.OfType<Motorcycle>()              
                .ToList();

            return View(motos);

        }

        [HttpGet]
        public ActionResult CreateCar()
        {
            CreateViewModel cvm = new CreateViewModel();
            return View(cvm);
        }

        [HttpPost]
        public ActionResult CreateCar(CreateViewModel cvm)
        {
            if (!ModelState.IsValid)
                return View(cvm);

            VehicleService.Instance.CreateCar(cvm.Make, cvm.Model1, cvm.Year);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult UpdateCar(int id)
        {
            Vehicle car = VehicleService.Instance.GetCar(id);

            UpdateViewModel model = new UpdateViewModel();
            model.Make = car.Make;
            model.Model1 = car.Model1;
            model.Year = car.Year;
            model.Id = car.Id;

            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateCar(UpdateViewModel model)
        {
            VehicleService.Instance.UpdateCar(model.Id, model.Make, model.Model1, model.Year);
            return RedirectToAction("Index");
        }
    }
}